//
//  iCalConduit.m
//  Task Views
//
//  Created by Chris Karr on 2/23/09.
//  Copyright 2009 Chris J. Karr. All rights reserved.
//

#import "iCalConduit.h"

#import <CalendarStore/CalendarStore.h>

@implementation iCalConduit

- (NSArray *) fetchTasks:(NSDictionary *) options
{
	CalCalendarStore * store = [CalCalendarStore defaultCalendarStore];
	NSPredicate * predicate = [CalCalendarStore taskPredicateWithCalendars:[store calendars]];
	NSArray * iCalTasks = [store tasksWithPredicate:predicate];
	
	NSMutableArray * tasks = [NSMutableArray array];

	NSEnumerator * taskIter = [iCalTasks objectEnumerator];
	CalTask * calTask = nil;
	while (calTask = [taskIter nextObject])
	{
		NSMutableDictionary * task = [NSMutableDictionary dictionary];

		[task setValue:[calTask completedDate] forKey:@"Completed Date"];
		[task setValue:[calTask dueDate] forKey:@"Due Date"];

		if ([calTask isCompleted])
			[task setValue:@"true" forKey:@"Completed"];
		else
			[task setValue:@"false" forKey:@"Completed"];

		[task setValue:[NSNumber numberWithUnsignedInteger:[calTask priority]] forKey:@"Priority"];

		[task setValue:[calTask dateStamp] forKey:@"Last Modified Date"];
		[task setValue:[calTask notes] forKey:@"Notes"];
		[task setValue:[calTask title] forKey:@"name"];
		[task setValue:[NSString stringWithFormat:@"iCal:%@", [calTask uid]] forKey:@"id"];
		[task setValue:[[calTask url] description] forKey:@"URL"];
		 
		CalCalendar * calendar = [calTask calendar];
		[task setValue:[calendar title] forKey:@"Calendar"];
		[task setValue:[calendar title] forKey:@"source"];
		 
		[tasks addObject:task];
	}
	
	return tasks;
}
		 
- (void) openExternalSite:(NSDictionary *) options
{
	[[NSWorkspace sharedWorkspace] launchApplication:@"iCal"];
}

@end
